package com.survey.service.impl;

import com.github.pagehelper.PageHelper;
import com.survey.dao.mapper.AnswersheetMapper;
import com.survey.pojo.data.AnswersheetDO;
import com.survey.pojo.vo.SurveyVO;
import com.survey.service.AnswersheetService;
import com.survey.utils.PageBean;
import com.survey.utils.PageCodeException;
import com.survey.utils.Result;
import com.survey.pojo.vo.AnswersheetVO;
import org.apache.commons.beanutils.BeanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/** 
 * <br/>
 * Created by survey on 2018/08/03
 */
@Service("answersheetService")
public class AnswersheetServiceImpl implements AnswersheetService {

	private final static Logger LOG = LoggerFactory.getLogger(AnswersheetServiceImpl.class);

	@Resource
	private AnswersheetMapper answersheetMapper;

	@Override
	public Result addAnswersheet(AnswersheetVO answersheetVO) throws Exception {
		return null;
	}

	@Override
	public Result updateAnswersheet(AnswersheetVO answersheetVO) throws Exception {
		return null;
	}
	
	@Override
	public PageBean<AnswersheetVO> listAnswersheet(AnswersheetVO query) throws Exception{
		int count=answersheetMapper.countAnswersheet(query);
		int max = (count + query.getPageSize() - 1) / query.getPageSize();
		if (max == 0) {
			return null;
		}
		if (query.getPageCode() > max) {
			throw new PageCodeException("页码越界啦", 1);
		}
		PageHelper.startPage(query.getPageCode(), query.getPageSize());
        List<AnswersheetDO> answersheetDOS = answersheetMapper.listAnswersheet(query);
        ArrayList answersheetVOS=new ArrayList();
        for (AnswersheetDO answersheetDO:answersheetDOS){
            AnswersheetVO answersheetVO=new AnswersheetVO();
            BeanUtils.copyProperties(answersheetVO,answersheetDO);
            answersheetVOS.add(answersheetVO);
        }
        PageBean<AnswersheetVO> pageBean = new PageBean<AnswersheetVO>(answersheetVOS, query.getPageSize()
                , query.getPageCode(), count, query.getSize());
        return pageBean;
	}

	@Override
	public Result findDetailAnswersheet(AnswersheetVO answersheetVO) throws Exception{
		return null;
	}
	
	@Override
	public Result listAnswersheetPage(AnswersheetVO answersheetVO) throws Exception{
		return null;
	}
	
	@Override
	public Result countAnswersheet(AnswersheetVO answersheetVO) throws Exception{
		return null;
	}
	
	@Override
	public Result deleteAnswersheet(AnswersheetVO answersheetVO) throws Exception{
		Integer integer = answersheetMapper.deleteAnswersheet(answersheetVO);
		if (integer>0){
			return Result.getSuccess("删除成功");
		}
		return Result.getFailure("删除失败");
	}

}